package com.lkj.youmeng.mvp.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lkj.youmeng.R;
import com.lkj.youmeng.adapter.RecommendAdapter;
import com.lkj.youmeng.base.BaseFragment;
import com.lkj.youmeng.data.HomeData;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

public class RecommendFragment extends BaseFragment {
    private String[] mImgUrls = new String[]{"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg14.360buyimg.com%2Fn0%2Fjfs%2Ft1%2F101755%2F10%2F970%2F584200%2F5db836ffE4956e001%2F5ee16a9e948525b4.jpg&refer=http%3A%2F%2Fimg14.360buyimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559780&t=edd1fac50ff91fa3c3ef66c01b64e5f8",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1514676603,950650736&fm=26&gp=0.jpg", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11290027587%2F0.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559833&t=c782d71dced9f1e039d8c60863fd5a60",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fwallpaper%2F1204%2F27%2Fc5%2F11401279_1335506542548_800x800.jpg&refer=http%3A%2F%2Fimg.pconline.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559833&t=0224772ebd99f476c3b8a3f7925e03fc",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg12.360buyimg.com%2FpopWaterMark%2Fjfs%2Ft26623%2F286%2F2440717986%2F387762%2F1a14d772%2F5c0161deNcd43292c.jpg&refer=http%3A%2F%2Fimg12.360buyimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559833&t=c58f4cde342b3666dcbbe82024758600",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg11.360buyimg.com%2Fn0%2Fjfs%2Ft1%2F21331%2F17%2F15809%2F512759%2F5cb29ec1E9ce9e207%2F807f6c4de9c4e124.jpg&refer=http%3A%2F%2Fimg11.360buyimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559833&t=9d86b0e7da002ec6fef2450be7ea16e6",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg10.360buyimg.com%2Fn0%2Fjfs%2Ft1%2F86062%2F23%2F1056%2F660089%2F5db836f0E2b5a9e7e%2F2ea2d3b38205e44d.jpg&refer=http%3A%2F%2Fimg10.360buyimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559833&t=376ec6a3931f0f3b2b09b500bd99833e",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg12.360buyimg.com%2Fn0%2Fjfs%2Ft1%2F57093%2F23%2F7337%2F232227%2F5d4f8481E22564c45%2F86f0948269fbf880.jpg&refer=http%3A%2F%2Fimg12.360buyimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559833&t=cd792883ca3fb9905e2f832472ffa8f6",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1566818772,763906969&fm=26&gp=0.jpg"};
    //    private String[] mImgUrls = new String[]{ "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11290027587%2F0.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618559833&t=c782d71dced9f1e039d8c60863fd5a60"};
    private ArrayList<HomeData> mData = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;
    private ClassicsHeader header;
    private ClassicsFooter footer;
    private RecyclerView recyclerView;
    private RecommendAdapter adapter;
    private ProgressBar progressBar;
    private boolean loading = false;
    private ArrayList<Bitmap> mBitmapList = new ArrayList<>();
    private int count = 0;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    loading = false;
                    progressBar.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    recyclerView.getAdapter().notifyDataSetChanged();
                    break;
                case 2:
                    final int id = (int) msg.obj;
                    Glide.with(getActivity()).asBitmap().load(mImgUrls[id]).into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            mBitmapList.add(resource);
                            mData.add(new HomeData(id + 1, resource, "标题" + id));
                            int len = id + 1;
                            if (len < mImgUrls.length) {
                                Message message = new Message();
                                message.what = 2;
                                message.obj = len;
                                handler.sendMessage(message);
                            } else {
                                handler.sendEmptyMessage(1);
                            }
                        }
                    });
                    break;
            }
        }
    };

    public static RecommendFragment newInstance(Bundle bundle) {
        RecommendFragment fragment = new RecommendFragment();
        if (bundle != null) {
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        initView(view);
        initRecycler();
        initData();
        return view;
    }

    private void initRecycler() {
        adapter = new RecommendAdapter(getContext(), mData);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(null);
        //垂直方向的2列
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        if (mImgUrls.length > 0){
            Message message = new Message();
            message.what = 2;
            message.obj = 0;
            handler.sendMessage(message);
        }
    }

    private void initView(View view) {
        refreshLayout = view.findViewById(R.id.refresh);
        header = view.findViewById(R.id.header);
        recyclerView = view.findViewById(R.id.recycler);
        footer = view.findViewById(R.id.foot);
        progressBar = view.findViewById(R.id.progress);
        progressBar.setVisibility(View.VISIBLE);
        loading = true;
        recyclerView.setVisibility(View.GONE);
        refreshLayout.setRefreshHeader(header);
        refreshLayout.setRefreshFooter(footer);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });

    }
}
